#pragma once

#include "elib_sdk/lib2.h"
#include "elib_sdk/fnshare.h"
#include "epugixml.h"
#include "pugixml.hpp"
#include <sstream>

class CFreqMem
{
private:
	unsigned char* m_pdata;
	size_t m_datalen, m_bufferlen;
public:
	CFreqMem()
	{
		m_pdata = NULL; m_datalen = m_bufferlen = 0;
	}
	void* GetPtr()
	{
		return (m_datalen == 0 ? NULL : m_pdata);
	}
	size_t GetDataLen() const
	{
		return m_datalen;
	}
	void AddDWord(DWORD dw)
	{
		AppendData(&dw, sizeof(dw));
	}
	void AppendData(const void* pdata, size_t size)
	{
		if (m_pdata == NULL)
		{
			if (size < 1024)
			{
				m_bufferlen = 1024;
			}
			else
			{
				m_bufferlen = size * 2;
			}
			m_pdata = (unsigned char*)MMalloc(m_bufferlen);
			//assert(m_datalen == 0);
		}
		else
		{
			size_t oldLen = m_bufferlen;
			while (m_bufferlen - m_datalen < size + 1)
			{
				m_bufferlen *= 2;
				unsigned char* newData = (unsigned char*)MMalloc(m_bufferlen);
				memcpy(newData, m_pdata, oldLen);
				MFree(m_pdata);
				m_pdata = newData;
			};
		}
		memcpy(m_pdata + m_datalen, pdata, size);
		m_datalen += size;
	}
};
struct xml_mem_writer : pugi::xml_writer
{
	CFreqMem m_result;
	virtual void write(const void* data, size_t size)
	{
		m_result.AppendData(data, size);
	}
	
};


void zySetPugiResult(pugi::xml_parse_result* res, PMDATA_INF pArgInf);